<?php

class EditCustomerForm extends BaseForm {

    public function __construct() {
        parent::__construct();
    }

    public function setup() {
        $titles = $this->getTitlesFormatted();

        $this->setWidgets(array(
            'customer_name' => new sfWidgetFormInputText(),
            'old_balance' => new sfWidgetFormInputText(),
            'shipping_address' => new sfWidgetFormInputText(),
            'billing_address' => new sfWidgetFormInputText(),
            'email' => new sfWidgetFormInputText(),
            'notes' => new sfWidgetFormTextarea(),
            'payment_history' => new sfWidgetFormChoice(array('choices' => array('poor' => 'poor', 'good' => 'good', 'excellent' => 'excellent'))),
            'payment_method' => new sfWidgetFormChoice(array('choices' => array('cash' => 'cash', 'cheque' => 'cheque', 'open account' => 'open account')))
        ));

        $this->setValidators(array(
            'customer_name' => new sfValidatorString(array('required' => true), array('required' => 'Please enter required field.')),
            'shipping_address' => new sfValidatorString(array('required' => true), array('required' => 'Please enter required field.')),
            'billing_address' => new sfValidatorString(array('required' => true), array('required' => 'Please enter required field.')),
            'old_balance' => new sfValidatorNumber(array('required' => true), array('required' => 'Please enter required field.')),
            'email' => new sfValidatorRegex(array('required' => false, 'pattern' => '/^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'), array('invalid' => 'Invalid E-mail.', 'required' => 'Please enter required field.')),
            'notes' => new sfValidatorString(array('required' => false), array("required" => "Please type your notes.")),
            'payment_history' => new sfValidatorChoice(array('choices' => array_keys(array('poor' => 'poor', 'good' => 'good', 'excellent' => 'excellent')), 'required' => true), array('required' => 'Please enter required field.')),
            'payment_method' => new sfValidatorChoice(array('choices' => array_keys(array('cash' => 'cash', 'cheque' => 'cheque', 'open account' => 'open account')), 'required' => true), array('required' => 'Please enter required field.'))
        ));

        $this->widgetSchema->setNameFormat('customer[%s]');
    }

    private function getTitlesFormatted() {
        $bank_accounts = Doctrine_Query::create()->from('Title')->fetchArray();
        $result = array('' => '');

        foreach ($bank_accounts as $bank_account) {
            $result[$bank_account['id']] = $bank_account['title_name'];
        }
        return $result;
    }

}

?>
